package com.market.dao.jpa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.market.dao.ProductDao;
import com.market.dao.PurchaseDetailDao;
import com.market.domain.Product;
import com.market.domain.PurchaseDetail;
import com.market.exception.OutOfStockException;

@Repository("purchaseDetailDao")
public class PurchaseDetailDaoJpa extends GenericDaoJpa<PurchaseDetail, Long>
		implements PurchaseDetailDao {

	@Autowired
	private ProductDao productDao;

	public PurchaseDetailDaoJpa() {
		super(PurchaseDetail.class);
	}

	public void remove(PurchaseDetail object) {
		if (super.findById(object.getId()) == null) {
			log.debug(this.getClass() + " Purchase detail with id "
					+ object.getId() + " is not found, can not be remove");
		} else {
			log.debug(this.getClass() + " Purchase detail with id "
					+ object.getId() + " is found, will be remove");
			super.removeById(object.getId());
		}
	}

	public PurchaseDetail saveAndUpdateInventory(PurchaseDetail purchaseDetail) {
		Product product = this.productDao.findById(purchaseDetail.getProduct()
				.getId());
		product.setInventory(product.getInventory()
				+ purchaseDetail.getReceivedQuantity()
				* product.getPurchaseToPriceSpec());
		this.productDao.save(product);
		purchaseDetail.setProduct(product);
		return super.save(purchaseDetail);
	}

	public PurchaseDetail removeAndUpdateInventory(PurchaseDetail purchaseDetail) {
		Product product = this.productDao.findById(purchaseDetail.getProduct()
				.getId());
		Long newInventory = product.getInventory()
				- purchaseDetail.getReceivedQuantity()
				* product.getPurchaseToPriceSpec();
		if (newInventory >= 0) {
			product.setInventory(newInventory);
			this.productDao.save(product);
			purchaseDetail.setProduct(product);
			super.removeById(purchaseDetail.getId());
		} else {
			throw new OutOfStockException(product.getNumber(),
					product.getName(), product.getInventory());
		}
		return purchaseDetail;
	}

	public PurchaseDetail process(PurchaseDetail purchaseDetail) {
		Product product = this.productDao.findById(purchaseDetail.getProduct()
				.getId());
		product.setInventory(product.getInventory()
				+ purchaseDetail.getReceivedQuantity()
				* product.getPurchaseToPriceSpec());
		product = this.productDao.save(product);
		purchaseDetail.setProduct(product);
		return purchaseDetail;
	}

	public PurchaseDetail unProcess(PurchaseDetail purchaseDetail) {
		Product product = this.productDao.findById(purchaseDetail.getProduct()
				.getId());
		Long newInventory = product.getInventory()
				- purchaseDetail.getReceivedQuantity()
				* product.getPurchaseToPriceSpec();
		if (newInventory >= 0) {
			product.setInventory(newInventory);
			this.productDao.save(product);
			purchaseDetail.setProduct(product);
		} else {
			throw new OutOfStockException(product.getNumber(),
					product.getName(), product.getInventory());
		}
		return purchaseDetail;
	}
	
	
	

}
